﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    /// <summary>
    /// 主窗体
    /// </summary>
    public partial class FrmMain : FrmBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();
            GlobalContext.MainForm = this;
        }

        /// <summary>
        /// 窗体载入函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            var items = new Dictionary<FrmBase, string>()
            {
                {new FrmMyInfo(), "用户信息"},
                {new FrmInvite(), "好友邀请"},
                {new FrmMyPupils(), "我的徒弟"},
                {new FrmMyIncome("balance"), "收入明细(零钱)"},
                {new FrmMyIncome("coins"), "收入明细(金币)"},
                {new FrmMission(), "新手任务"},
                {new FrmExchangeProduct(), "兑换商品"},
                {new FrmExchangeRecord(), "兑换记录"},
                {new FrmReadArticleManual(), "阅读文章"},
                {new FrmReadArticleHalfAuto(), "半自动阅读"},
                {new FrmBatchOpe(), "批量操作"},
                //{new FrmTest(), "测试"}
            };

            foreach (KeyValuePair<FrmBase, string> item in items)
            {
                var tabPage = new TabPage(item.Value);
                this.tabControl1.TabPages.Add(tabPage);
                AttachFormToPanel(item.Key, tabPage);
            }

            InitNotifyIcon();
        }

        private void AttachFormToPanel(Form frm, Panel panel)
        {
            frm.TopLevel = false;
            frm.Parent = panel;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.WindowState = FormWindowState.Normal;
            frm.Visible = true;
            frm.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// “登录”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonLogin_Click(object sender, EventArgs e)
        {
            var frm = new FrmLogin();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                new Thread(() =>
                {
                    try
                    {
                        var result = QttHelper.GetMemberInfo(frm.MemberToken);
                        if (result.Code != 0)
                        {
                            MsgBox.ShowInfo("登录失败，" + result.Message);
                            return;
                        }
                        GlobalContext.NewRegisterMobilePwdCache.Remove(result.Data.MobileNumber);
                        GlobalContext.MemberInfo = result.Data;
                        GlobalContext.MemberToken = frm.MemberToken;
                        ConfigUtil.SaveUserInfo(base.MemberInfo.NickName,
                                                base.MemeberToken,
                                                base.MemberInfo.MobileNumber,
                                                base.MemberInfo.MemberId,
                                                base.MemberInfo.TeacherId);
                                                

                        base.ChangeMainTitle(base.AppName + " - " + GlobalContext.MemberInfo.NickName);
                    }
                    catch (Exception ex)
                    {
                        MsgBox.ShowInfo("登录失败，" + ex.Message);
                    }
                }).Start();
            }
        }

        private void toolStripButtonReg_Click(object sender, EventArgs e)
        {
            new FrmReg().Show();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void toolStripButtonProxySetting_Click(object sender, EventArgs e)
        {
            new FrmProxy().Show();
        }

        private void toolStripButtonManageDeviceCode_Click(object sender, EventArgs e)
        {
            new FrmDeviceCode().Show();
        }

        private void toolStripButtonBatchReg_Click(object sender, EventArgs e)
        {
            new FrmBatchReg().Show();
        }

        private void InitNotifyIcon()
        {
            this.notifyIcon1.Icon = this.Icon;
            this.notifyIcon1.Text = this.Text;
        }

        private void FrmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Show();
            }
        }
    }
}
